package com.bicon.botu.mall.goods.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.goods.entity.ProductPicture;
import com.bicon.botu.mall.goods.mapper.ProductPictureMapper;
import com.bicon.botu.mall.goods.service.IProductPictureService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 孙永刚
 * @since 2017-07-19
 */
@Service
public class ProductPictureServiceImpl extends ServiceImpl<ProductPictureMapper, ProductPicture> implements IProductPictureService {


    @Override
    public boolean addPicture(String pictureStr, String productId, String opertion) {
        List<ProductPicture> pictrueList = new ArrayList<>();
        new ProductPicture().delete(new EntityWrapper().eq("product_id", productId));
        if (!StringUtils.isEmpty(pictureStr)) {
            List<ProductPicture> list = JSONObject.parseArray(pictureStr, ProductPicture.class);
            int i = 0;
            for (ProductPicture pictrue : list) {

                pictrue.preInsert();
                pictrue.setProductId(productId);
                if (i == 0) {
                    pictrue.setIsMain("1");
                }
                pictrue.insert();
                i++;
            }
        }

        return true;
    }


    @Override
    public boolean addPicture(String pictureStr, String productId) {
        new ProductPicture().delete(new EntityWrapper().eq("product_id", productId));
        if (!StringUtils.isEmpty(pictureStr)) {
            List<ProductPicture> list = JSONObject.parseArray(pictureStr, ProductPicture.class);
            List<ProductPicture> result = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                ProductPicture picture = list.get(i);
                if (i == 0) {
                    picture.setIsMain("1");
                }
                result.add(picture);
            }
            return insertBatch(result);
        }
        return false;
    }
}
